import React, { Fragment } from 'react';
import { connect } from 'dva';
import BasePage from './BasePage'
import CurrentLocation from '../components/currentLocation'
import breadArr from '../constant/breadArr'
import PatentDataTable from '../components/table/patentDataTable'
import { Space, Button, Upload, Modal, Form, Input, Select, Popconfirm, message, Spin } from 'antd'
import constant from '../constant/constant'
import patentDataTable from '../components/table/patentDataTable';
import RegionalDistributionChart from '../components/chart/regionalDistributionChart'
import YearAnalysisChart from '../components/chart/yearAnalysisChart'
import MechanismChart from '../components/chart/mechanismChart'
import PatentDataSearchForm from '../components/patentDataSearchForm'
import TechnologyClusteringGraph from '../components/chart/technologyClusteringGraph'
class PatentDataSearchPage extends React.Component {
    constructor(props) {
        super(props),
            this.state = {
                showRegionalDistributionChart: false,
                showYearAnalysisChart: false,
                showMechanismChart: false,
                showTechnologyClustering: false,
            }
    }

    //地区分类分析
    showRegionalDistributionChart = () => {
        if (this.props.selectedIds.length == 0) {
            message.error("请先选中专利数据")
            return
        }

        let resultData = statisticalRegionalDistributionData(this.props.selectedRows)

        this.props.dispatch({
            type: "patentData/setRegionalDistributionChartData",
            data: resultData
        })

        this.setState({
            showRegionalDistributionChart: true
        })
    }

    showYearAnalysisChart = () => {
        if (this.props.selectedIds.length == 0) {
            message.error("请先选中专利数据")
            return
        }

        let resultData = statisticalYearAnalysisData(this.props.selectedRows)

        this.props.dispatch({
            type: "patentData/setYearAnalysisChartData",
            data: resultData
        })

        this.setState({
            showYearAnalysisChart: true
        })
    }

    showMechanismChart = () => {
        if (this.props.selectedIds.length == 0) {
            message.error("请先选中专利数据")
            return
        }

        let resultData = statisticalMechanismData(this.props.selectedRows)

        this.props.dispatch({
            type: "patentData/setMechanismChartData",
            data: resultData
        })

        this.setState({
            showMechanismChart: true
        })
    }

    showOffRegionalDistributionChart = () => {
        this.setState({
            showRegionalDistributionChart: false
        })

        this.props.dispatch({
            type: "patentData/setMechanismData",
            data: {
                companyData: [],
                schoolData: []
            }
        })
    }

    showOffYearAnalysisChart = () => {
        this.setState({
            showYearAnalysisChart: false
        })

        this.props.dispatch({
            type: "patentData/setRegionalDistributionChartData",
            data: []
        })
    }

    showOffMechanismChart = () => {
        this.setState({
            showMechanismChart: false
        })

        this.props.dispatch({
            type: "patentData/setMechanismChartData",
            data: []
        })
    }

    showTechnologyHotspot = () => {
        if (this.props.selectedRows.length == 0) {
            message.error("请先选择一条专利数据！")
            return
        }

        this.props.dispatch({
            type: "patentData/getTechnologyHotspot",
            data: {
                description: this.props.abstractList
            }
        })

    }

    //聚类分析
    showTechnologyClustering = () => {
        if (this.props.selectedRows.length == 0) {
            message.error("请先选择一条专利数据！")
            return
        }
        //  周楷
        this.props.dispatch({
            type: "patentData/getTechnologyClustering",
            data: {
                description: this.props.abstractList
            }
        }).then(() => {
            this.setState({
                showTechnologyClustering: true
            })
        })


    }

    //技术聚类
    showOffTechnologyClusteringChart = () => {
        this.setState({
            showTechnologyClustering: false
        })
    }


    render() {
        return (
            <Spin spinning={this.props.technologyClustering}>
                <BasePage backGroundPictureUrl={require('../assets/imgs/patentData.png')}>
                    <CurrentLocation breadArr={breadArr.PatentDataSearchPage}></CurrentLocation>
                    <Space size='large' style={{ marginLeft: 572 }}>
                        {/* <Button type="primary" onClick={this.showTechnologyHotspot}>技术热点分析</Button> */}
                        <Button type="primary" onClick={this.showTechnologyClustering}>技术聚类分析</Button>
                        <Button type="primary" onClick={this.showRegionalDistributionChart}>地区分布分析</Button>
                        <Button type="primary" onClick={this.showYearAnalysisChart}>申请年份分析</Button>
                        <Button type="primary" onClick={this.showMechanismChart}>机构分析</Button>
                    </Space>
                    <div style={{ marginTop: 20 }}><PatentDataSearchForm></PatentDataSearchForm></div>
                    <div style={{ marginTop: 20 }}><PatentDataTable></PatentDataTable></div>
                    <Modal
                        title={"技术聚类分析"}
                        visible={this.state.showTechnologyClustering}
                        onOk={this.showOffTechnologyClusteringChart}
                        onCancel={this.showOffTechnologyClusteringChart}
                        width={980}
                        destroyOnClose={true}
                    >
                        <TechnologyClusteringGraph></TechnologyClusteringGraph>
                    </Modal>
                    <Modal
                        title={"地区分布"}
                        visible={this.state.showRegionalDistributionChart}
                        onOk={this.showOffRegionalDistributionChart}
                        onCancel={this.showOffRegionalDistributionChart}
                        width={800}
                        destroyOnClose={true}
                    >
                        <RegionalDistributionChart></RegionalDistributionChart>
                    </Modal>

                    <Modal
                        title={"申请年份"}
                        visible={this.state.showYearAnalysisChart}
                        onOk={this.showOffYearAnalysisChart}
                        onCancel={this.showOffYearAnalysisChart}
                        width={800}
                        destroyOnClose={true}
                    >
                        <YearAnalysisChart></YearAnalysisChart>
                    </Modal>

                    <Modal
                        title={"机构"}
                        visible={this.state.showMechanismChart}
                        onOk={this.showOffMechanismChart}
                        onCancel={this.showOffMechanismChart}
                        width={600}
                        destroyOnClose={true}
                    >
                        <MechanismChart type={1}></MechanismChart>
                        <MechanismChart type={2}></MechanismChart>
                    </Modal>
                </BasePage>
            </Spin>
        )
    }
}

const mapStateToProps = state => {
    let selectedIds = state.patentData.selectedIds
    let selectedRows = state.patentData.selectedRows
    let abstractList = state.patentData.abstractList
    let technologyClustering = state.loading.effects['patentData/getTechnologyClustering'] || false
    return {
        selectedIds,
        selectedRows,
        abstractList,
        technologyClustering
    }
}

export default connect(mapStateToProps)(PatentDataSearchPage)

function statisticalRegionalDistributionData(selectedRows) {
    let data = []
    if (data.length == 0) {
        data.push({
            name: selectedRows[0].applicantProvince.substring(0, selectedRows[0].applicantProvince.indexOf(";")),
            value: 1
        })
    }

    for (let i = 1; i < selectedRows.length; i++) {
        let isAddData = false
        for (let j = 0; j < data.length; j++) {
            if (selectedRows[i].applicantProvince.substring(0, selectedRows[i].applicantProvince.indexOf(";"))
                == data[j].name) {
                data[j].value++
                break
            }

            if (j == data.length - 1) {
                isAddData = true
            }
        }

        if (isAddData) {
            data.push({
                name: selectedRows[i].applicantProvince.substring(0, selectedRows[i].applicantProvince.indexOf(";")),
                value: 1
            })
        }

    }
    return data
}

function statisticalYearAnalysisData(selectedRows) {
    let data = []
    if (data.length == 0) {
        data.push({
            year: selectedRows[0].applicationDate.substring(0, 4),
            value: 1
        })
    }

    for (let i = 1; i < selectedRows.length; i++) {
        let isAddData = false
        for (let j = 0; j < data.length; j++) {
            if (selectedRows[i].applicationDate.substring(0, 4)
                == data[j].year) {
                data[j].value++
                break
            }

            if (j == data.length - 1) {
                isAddData = true
            }
        }

        if (isAddData) {
            data.push({
                year: selectedRows[i].applicationDate.substring(0, 4),
                value: 1
            })
        }

    }
    data.sort(sortYear)
    return data
}

function statisticalMechanismData(selectedRows) {
    let data = {
        companyData: [],
        schoolData: []
    }

    for (let i = 0; i < selectedRows.length; i++) {
        if (selectedRows[i].applicantType == '企业') {
            let isAddData = false
            if (data.companyData.length == 0) {
                data.companyData.push({
                    applicant: selectedRows[i].applicant,
                    value: 1
                })
                continue;
            }

            for (let j = 0; j < data.companyData.length; j++) {
                if (selectedRows[i].applicant == data.companyData[j].applicant) {
                    data.companyData[j].value++
                    break
                }

                if (j == data.companyData.length - 1) {
                    isAddData = true
                }
            }

            if (isAddData) {
                data.companyData.push({
                    applicant: selectedRows[i].applicant,
                    value: 1
                })
            }
        }

        if (selectedRows[i].applicantType == "大专院校" || selectedRows[i].applicantType == "高校" || selectedRows[i].applicantType == "科研单位") {
            let isAddData = false
            if (data.schoolData.length == 0) {
                data.schoolData.push({
                    applicant: selectedRows[i].applicant,
                    value: 1
                })
                continue
            }

            for (let j = 0; j < data.schoolData.length; j++) {
                if (selectedRows[i].applicant == data.schoolData[j].applicant) {
                    data.schoolData[j].value++
                    break
                }

                if (j == data.schoolData.length - 1) {
                    isAddData = true
                }
            }

            if (isAddData) {
                data.schoolData.push({
                    applicant: selectedRows[i].applicant,
                    value: 1
                })
            }
        }
    }

    data.companyData.sort(sortValue)
    data.schoolData.sort(sortValue)
    return data
}

function sortYear(a, b) {
    return a.year - b.year
}

function sortValue(a, b) {
    return b.value - a.value
}

